package com.tencent.sr.iris.basic.mp.service.task;

import com.tencent.sr.iris.activity.interfaces.request.task.GroupTaskListNewRequest;
import com.tencent.sr.iris.activity.interfaces.request.task.MarketTaskCalenderRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.task.GroupTaskListResponse;
import com.tencent.sr.iris.basic.mp.common.config.GroupTaskConfig;
import com.tencent.sr.iris.basic.mp.common.util.GroupTaskConstant;
import com.tencent.sr.iris.basic.mp.common.util.TimeUtils;
import com.tencent.sr.iris.basic.mp.dependency.GroupTaskDependency;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class GroupTaskService {

    @Resource
    private RedisClient redisClient;
    @Resource
    private GroupTaskConfig groupTaskConfig;
    @Resource
    private GroupTaskDependency groupTaskDependency;

    public Boolean popCallBack(String uid) {
        String key = GroupTaskConstant.generatePopKey(uid);
        long expired = TimeUtils.getSecondsNextEarlyMorning();
        redisClient.set(key, "1", "nx", "ex", expired);
        return true;
    }

    public Boolean needPop(String uid) {
        String key = GroupTaskConstant.generatePopKey(uid);
        Boolean exists = redisClient.exists(key);
        return Boolean.TRUE.equals(groupTaskConfig.getPopSwitch()) && !exists;
    }

    public PaginationResponse<GroupTaskListResponse> listInProgress(
            PaginationRequest<GroupTaskListNewRequest> request) {
        PaginationResponse<GroupTaskListResponse> page = groupTaskDependency.listInProgress(request);
        return page;
    }

    public PaginationResponse<GroupTaskListResponse> listEnded(
            PaginationRequest<GroupTaskListNewRequest> request) {
        PaginationResponse<GroupTaskListResponse> page = groupTaskDependency.listEnded(request);
        return page;
    }

    public List<GroupTaskListResponse> marketCalendarList(MarketTaskCalenderRequest request) {
        return groupTaskDependency.marketCalendarList(request);
    }
}
